
#include<iostream>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include<unistd.h>
#include<string.h>
#include <arpa/inet.h>
using namespace std;

class UdpClient{
  public:

    UdpClient(string server_ip,int server_port)
      :_sockfd(-1)
    ,_server_ip(server_ip)
    ,_server_port(server_port)
    {}



    bool InitClient(){

      _sockfd = socket(AF_INET,SOCK_DGRAM,0);
      if(_sockfd<0)
      {
        cerr<<"socket fail"<<endl;
        return false;
      }

      return true;
    }
    
    void Start()
    {
      string msg;
      struct sockaddr_in peer;
      memset(&peer,'\0',sizeof(peer));
      peer.sin_port=htons(_server_port);
      peer.sin_addr.s_addr=inet_addr(_server_ip.c_str());
      peer.sin_family = AF_INET;

      while(1)
      {
        cout<<"Please Enter# ";
        getline(cin,msg);
        sendto(_sockfd,msg.c_str(),msg.size(),0,(struct sockaddr*)&peer,sizeof(peer));
      }
    }

  private:
    int _sockfd;
    string _server_ip;
    int _server_port;
};


int main(int argc,char* argv[])
{
  if(argc!=3)
  {
    cerr<<"Usage: "<<argv[0]<<" server_ip server_port"<<endl;
    return 1;
  }
  string server_ip=argv[1];
  int server_port =atoi(argv[2]);
  UdpClient* clt=new UdpClient(server_ip,server_port);
  clt->InitClient();
  clt->Start();
  return 0;
}
